package com.summary.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.DateUtils;
import com.summary.domain.WeekTime;
import com.summary.mapper.WeekTimeMapper;
import org.springframework.stereotype.Service;
import com.summary.mapper.WeekSummaryMapper;
import com.summary.domain.WeekSummary;
import com.summary.service.IWeekSummaryService;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 周报Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-15
 */
@Service
public class WeekSummaryServiceImpl implements IWeekSummaryService
{
    @Resource
    private WeekSummaryMapper weekSummaryMapper;

    @Resource
    private WeekTimeMapper weekTimeMapper;

    /**
     * 查询周报
     *
     * @return 周报
     */
    @Override
    public List<WeekSummary> selectWeekSummaryById(Long timeId)
    {
        return weekSummaryMapper.selectWeekSummaryById(timeId);
    }

    /**
     * 查询周报列表
     *
     * @param weekSummary 周报
     * @return 周报
     */
    @Override
    public List<WeekSummary> selectWeekSummaryList(WeekSummary weekSummary)
    {
        return weekSummaryMapper.selectWeekSummaryList(weekSummary);
    }

    /**
     * 新增周报
     *
     * @param weekSummary 周报
     * @return 结果
     */
    @Override
    public int insertWeekSummary(WeekSummary weekSummary)
    {
        weekSummary.setCreateTime(DateUtils.getNowDate());
        return weekSummaryMapper.insertWeekSummary(weekSummary);
    }

    /**
     * 修改周报
     *
     * @param weekSummary 周报
     * @return 结果
     */
    @Override
    public int updateWeekSummary(WeekSummary weekSummary)
    {
        return weekSummaryMapper.updateWeekSummary(weekSummary);
    }

    /**
     * 批量删除周报
     *
     * @param ids 需要删除的周报主键
     * @return 结果
     */
    @Override
    public int deleteWeekSummaryByIds(Long[] ids)
    {
        return weekSummaryMapper.deleteWeekSummaryByIds(ids);
    }

    /**
     * 删除周报信息
     *
     * @param id 周报主键
     * @return 结果
     */
    @Override
    public int deleteWeekSummaryById(Long id)
    {
        return weekSummaryMapper.deleteWeekSummaryById(id);
    }


    /**
     * 根据userId查询时间段内是否填写周报
     *
     * @param userId
     * @param pageDomain
     * @return
     */
    @Override
    public List<WeekSummary> getInfoByUserId(long userId, PageDomain pageDomain) {
        List<WeekSummary> weekSummaryList = new ArrayList<>();
        //1.查询所有的时间段，拿到timeId数组
        Long[] timeIds = weekTimeMapper.selectWeekTimeIdList();
        //2.根据userId和timeId查询所有的时间段内是否填写周报
        for (int i = 0;i < timeIds.length;i++){
            WeekSummary weekSummary = new WeekSummary();
            weekSummary.setUserId(userId);
            weekSummary.setTimeId(timeIds[i]);
            startPage();
            WeekSummary weekSummary1 = weekSummaryMapper.selectWeekSummary(weekSummary);
            WeekTime weekTime = weekTimeMapper.selectWeekTimeById(timeIds[i]);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); //2023-10-23 13:30:53
            String startTime = sdf1.format(weekTime.getStartTime());
            String endTime = sdf1.format(weekTime.getEndTime());
            weekSummary.setStartTime(startTime);
            weekSummary.setEndTime(endTime);
            weekSummary.setNumber(weekTime.getNumber());
            if (weekSummary1 != null){
                    weekSummary.setBriefSummary(weekSummary1.getBriefSummary());
                    weekSummary.setId(weekSummary1.getId());
            }
            weekSummaryList.add(weekSummary);
        }
        return weekSummaryList;
    }
}
